﻿using System;
using System.Collections.Generic;
using TuxedoLib;

namespace SpaceSquirrel
{
	public class IntroScene : GameSceneBase
	{
		private int page;
		private Image[] pages;
		private int counter = 0;

		public IntroScene()
			: base()
		{
			this.page = 0;
			this.pages = new Image[] {
				ImageLibrary.GetImage("slides/open1.png"),
				ImageLibrary.GetImage("slides/open2.png"),
				ImageLibrary.GetImage("slides/open3.png"),
			};
		}

		public override void ProcessInput(List<MyEvent> events, Dictionary<string, bool> pressedActions)
		{
			
		}

		public override void Update()
		{
			JukeBox.EnsureSong("intro");

			++this.counter;

			if (this.counter == 10)
			{
				this.Next = new DialogScene(this, "Open1", false, true, true);
			}
			else if (this.counter == 15)
			{
				this.page = 1;
			}
			else if (this.counter == 20)
			{
				this.Next = new DialogScene(this, "Open2", false, true, true);
			}
			else if (this.counter == 25)
			{
				this.page = 2;
			}
			else if (this.counter == 30)
			{
				this.Next = new DialogScene(this, "Open3", false, true, false);
			}
			else if (this.counter == 35)
			{
				this.Next = this.GetStartScene();
			}
		}

		public override void Render(TuxedoLib.Image screen, int renderCounter)
		{
			screen.Fill(0, 0, 0);
			screen.Blit(this.pages[this.page], 0, 16);
		}

		public GameSceneBase GetStartScene()
		{
			string fastStart = "C:\\users\\blake\\desktop\\fast_start.txt";
			if (System.IO.File.Exists(fastStart))
			{
				string text = System.IO.File.ReadAllText(fastStart);
				int i = 0;
				bool ignore = false;
				string level = null;
				int x = 2;
				int y = 2;
				List<string> flags = new List<string>();
				foreach (string rawLine in text.Split('\n'))
				{
					string line = rawLine.Split('#')[0].Trim();
					if (line.Length > 0)
					{
						i++;
						if (i == 1)
						{
							if (line == "IGNORE")
							{
								ignore = true;
								break;
							}
							level = line;
						}
						else if (i == 2)
						{
							string[] parts = line.Split(',');
							x = int.Parse(parts[0].Trim());
							y = int.Parse(parts[1].Trim());
						}
						else
						{
							flags.Add(line.Trim().ToLowerInvariant());
						}
					}
				}

				if (!ignore)
				{
					Context context = new Context();
					foreach (string flag in flags)
					{
						switch (flag)
						{
							case "gravity": context.Gravity = true; break;
							case "volcano1": context.VolcanoA = true; break;
							case "volcano2": context.VolcanoB = true; break;
							case "volcano3": context.VolcanoC = true; break;
							case "water1": context.BalloonA = true; break;
							case "water2": context.BalloonB = true; break;
							case "water3": context.BalloonC = true; break;
							case "water4": context.BalloonD = true; break;
							default: break;
						}
					}

					return new PlayScene(level, x, y, context);
				}
			}


			return new PlayScene("ship_1", 8, 9, new Context());
		}
	}
}
